package com.system.web;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.auth.domain.T_function;
import com.auth.domain.T_role_function;
import com.auth.domain.T_user_function;
import com.auth.domain.T_user_role;
import com.auth.mapper.T_functionMapper;
import com.auth.mapper.T_role_functionMapper;
import com.auth.mapper.T_user_functionMapper;
import com.auth.mapper.T_user_roleMapper;
import com.auth.web.spring.AuthUtils;
import com.system.service.LogonService;

/**
 * 权限处理
 *
 * @author davexin
 */
//@Service
//@Transactional
@Controller

public class AuthorizationController {

	private static Logger log = Logger.getLogger(AuthorizationController.class.getName());

	@Resource
	private LogonService logonService;

	@RequestMapping(value = "/getAuthFunction")
	@ResponseBody
	public List getPower(HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes){
		List retList = new ArrayList();

		List functionList = null;
		T_user_function uf = new T_user_function();
		uf.setStartNo(0);
		uf.setEndNo(10000);

		T_user_role ur = new T_user_role();
		ur.setStartNo(0);
		ur.setEndNo(10000);

		T_role_function rf = new T_role_function();
		rf.setStartNo(0);
		rf.setEndNo(10000);

		T_function function = new T_function();
		function.setStartNo(0);
		function.setEndNo(10000);

		//Map authMap = (Map)request.getSession().getAttribute("FREE_USER_AUTH");
		//modify by davexin @20161126, 防止ie8 redirector后，取不到session的 问题；
		//session中保存的functid对应map信息；
	    HttpSession session=request.getSession(false);
	    Map authMap = (Map)session.getAttribute("FREE_USER_AUTH");
		
		Map functionMap = new HashMap();
		int functid = -1;
		List userFunctList = new ArrayList();
		List rflist = new ArrayList();
		try{
			String tempBathPath= request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort() +""+ request.getContextPath()+"/";
			String userid = (String) request.getSession().getAttribute("FREE_CURRENT_USER");
			uf.setUserid(userid);

			//1、首先查询出全部function数据，存放到functionMap中;数据是按照 functlevel、showseq 升序排列；
			T_function ta = new T_function();
			ta.setFunctid(0); //从根目录开始查询；
			ta.setStartNo(0);
			ta.setEndNo(10000);
			ta.setStatus("1"); //有效状态，此处设置无效，因为查询条件中没有该条件，只支持functid查询条件；需要在登录时的数据中过滤无效菜单；
			functionList = logonService.findAllFunction(ta);
		    Iterator tempfl = functionList.iterator();
            while(tempfl.hasNext()){
            	ta =null;
            	ta = (T_function)tempfl.next();
            	functionMap.put(ta.getFunctid(),ta);
            	log.debug("------系统有效菜单functid="+ta.getFunctid()+"；functname="+ta.getFunctname());
            }

			//2、按照树形菜单顺序，根据权限map中的数据，删除没有权限的功能菜单，返回所有不重复的所有t_function数据；并且排列成一个树形菜单顺序输出，页面上只需要顺序输出就可以；
			List sortlist = AuthUtils.sortFunctionList(functionList);
			T_function mf = null;
			Iterator itmenu = sortlist.iterator();
			while(itmenu.hasNext()){
				mf = (T_function)itmenu.next();
				log.error("<<<<<<<<authMap="+authMap+">>>>>>>>>>>>>>mf.getFunctid()="+ mf.getFunctid());
				log.error("?????????????session id2="+request.getSession().getId());
				//如果权限中存在，则
				if(authMap.containsKey(mf.getFunctid())){
					//sortlist.remove(mf);//此方法会报错；
					retList.add(mf);
					log.debug("------最终计算出权限数据为：functid="+mf.getFunctid()+";functlevel="+mf.getFunctlevel()+";functname="+mf.getFunctname());
				}
			}
		}catch(Exception e){
			log.error(e);
			e.printStackTrace();
			log.error("系统出错，请联系管理员 ！");
		}
		return retList;
	}

	@RequestMapping(value = "/checkRight")
	@ResponseBody
	public boolean checkRight(HttpServletRequest request, HttpServletResponse response) {
		boolean hasRight = false;
		String funcname = request.getParameter("funcname");
		T_function f = null;
		List functList  = (List) request.getSession().getAttribute("FREE_USER_AUTH");
		if(functList!=null) {
			Iterator it = functList.iterator();
			while (it.hasNext()) {
				f = (T_function) it.next();
				if(f.getFunctname().equals(funcname)){
					hasRight = true;
					return hasRight;
				}
			}
		}

		return false;
	}
}
